// pages/taskList/taskList.js

import todo from '../../../components/v2/plugins/todo'
import selectable from '../../../components/v2/plugins/selectable'
import solarLunar from '../../../components/v2/plugins/solarLunar/index'
import timeRange from '../../../components/v2/plugins/time-range'
import week from '../../../components/v2/plugins/week'
import holidays from '../../../components/v2/plugins/holidays/index'
import plugin from '../../../components/v2/plugins/index'

plugin
  .use(todo)
  .use(solarLunar)
  .use(selectable)
  .use(week)
  .use(timeRange)
  .use(holidays)

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopName:'',
    storepromvisitlist:[],
    storepromvisitlistback:[],
    CustomBar:app.globalData.CustomBar,
    jinDu:0,
    weiDu:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMyLocation();
  },

/**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.search()

    const calendar = this.selectComponent('#calendar').calendar
    //calendar.setSelectedDates()
  },
  afterTapDate(e) {
    console.log(e.detail)

    let date = e.detail.year + "-" + (e.detail.month < 10 ? "0" + e.detail.month : e.detail.month) + "-" + (e.detail.date < 10 ? "0" + e.detail.date : e.detail.date);

    var newDates = [];
    console.log(date);
    for(let item of this.data.storepromvisitlist){
      if(item.scheduledate == date){
        newDates.push(item)
      }
    }

    this.setData({
      storepromvisitlistback : newDates
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  search:function(){
    let data = {'storename' :  this.data.shopName}
    let that = this;
    app.api.getStorepromvisitlist(data).then(res => {
      if(res.code == 200){
        for(let item of res.rows){
          try{
            let itu = JSON.parse(item.itude)
            let dis = this.getDisance(itu.latitude,itu.longitude,this.data.jinDu,this.data.weiDu)
            item.dis = dis
          }catch(e){
            console.log(e)
          }
        }

        const calendar = that.selectComponent('#calendar').calendar
        calendar.cancelSelectedDates()

        var alldate = [];
        for(let item of res.rows){
          var sarr = item.scheduledate.split("-");
          alldate.push({
            year: sarr[0],
            month: sarr[1],
            date: sarr[2]
          })
        }
        calendar.setTodos({
          // 待办点标记设置
          pos: 'bottom', // 待办点标记位置 ['top', 'bottom']
          dotColor: 'red', // 待办点标记颜色
          circle: true, // 待办圆圈标记设置（如圆圈标记已签到日期），该设置与点标记设置互斥
          showLabelAlways: false, // 点击时是否显示待办事项（圆点/文字），在 circle 为 true 及当日历配置 showLunar 为 true 时，此配置失效
          dates: alldate
        })

        that.setData({
          storepromvisitlistback : res.rows,
          storepromvisitlist : res.rows
        })
      }
    })
  },
  selectedItem:function(e){
    let index = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '../signInTask/signInTask?gid='+this.data.storepromvisitlistback[index].gid
    })
  },
  getMyLocation(){
    let that = this;
    wx.getLocation({
      type: 'gcj02',
      success:(res) =>{
        that.setData({
          jinDu : res.latitude, //经度
          weiDu : res.longitude //纬度
        })
        that.search()
      },
      fail : res => {
        if('getLocation:fail auth deny' == res.errMsg){
          console.log(res)
          wx.showModal({
            title: '提示',
            content: '获取用户定位授权失败',
            showCancel:false,
            success(res) {
              wx.navigateBack()
            }
          })
        }else if('getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF' == res.errMsg){
          wx.showModal({
            title: '提示',
            content: '请打开手机定位',
            showCancel:false,
            success(res) {
              wx.navigateBack()
            }
          })
        }else{
          wx.showToast({
            title: res.errMsg,
          })
        }
      }
    })
  },
  getDisance(la1, lo1, la2, lo2) {
    let La1 = la1 * Math.PI / 180.0;
    let La2 = la2 * Math.PI / 180.0;
    let La3 = La1 - La2;
    let Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    s = s.toFixed(2);
    return s;
  },
  inpShopName:function(e){
    this.setData({
      shopName:e.detail.value
    })
  },
})